var form, $,areaData;

layui.use(['form','layer','upload','laydate','jquery'],function(){
    var form = layui.form;
    var $ = layui.jquery;
    var layer = layui.layer;

    $.ajax({
        type : "post",
        url : "../department.do",
        data : {
            tran:'select',
            type:'self'
        },
        dataType : "json",
        success : function(f) {
            var tmp = '<option value="">--请选择部门--</option>';
            var d = f.data;
            $(".department").html(tmp);
            for ( var i in d) {
                tmp += '<option value="' + d[i].id +  '">' + d[i].departmentName + '</option>';
            }
            $(".department").html(tmp);
            form.render();
        }
    });

    form.on('select(department)', function(data) {
        var departmentId = data.value;
        $.ajax({
            type : "post",
            url : "../group.do",
            data : {
                tran:'select',
                type:'id',
                departmentId:departmentId
            },
            dataType : "json",
            success : function(f) {
                var tmp = '<option value="">--请选择组别--</option>';
                 var d = f.data;
                $(".group").html(tmp);
                for ( var i in d) {
                    tmp += '<option value="' + d[i].id +  '">' + d[i].group + '</option>';
                }
                $(".group").html(tmp);
                form.render();
            }
        });
    });

    function review(){
        $("#id").val("");
        $("#username").val("");
        $("#password").val("");
        $("#phone").val("");
    }
    //提交个人资料
    form.on("submit(changeUser)",function(data){
        $.ajax({
            type : "post",
            url : "../user.do",
            data: data.field,
            dataType : "json",
            success : function(f) {
                if (f.code == "0"){
                    layer.msg(f.msg,{icon: 1});
                    review();
                }else{
                    layer.msg(f.msg, {icon: 2});
                }
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })
    form.verify({
        veriff:[
            /[0-9]{10}/,
            '请输入10位学号'
        ],
        password:[
            /\S{6,}/,
            '密码不能少于6位'
        ],
        username:function (value) {
            var msg = null ;
            $.ajax({
                type : "post",
                url : "../UserServlet.do",
                data : {
                    "transaction":"checkUser",
                    "id":value.toString()
                },
                dataType : "json",
                async:false,
                success : function(f) {
                    if (f.code == "1"){
                       msg=f.msg;
                    }
                }
            });
            if (msg != null)
                return msg;
        }
    })
})